﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using Informator.Presentation.Common;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using Informator.Presentation.Nastava_i_vezbe;

namespace Informator.Presentation.Nastava_i_vezbe
{
    /// <summary>
    /// Interaction logic for pgNastava_i_vezbe.xaml
    /// </summary>
    public partial class pgNastava_i_vezbe : Page
    {
        private double grid_;
        private readonly DispatcherTimer _mainTimer;
        List<PredmetModel> listaPredmeta = new List<PredmetModel>();
        List<PredmetModel> listaVezbi=new List<PredmetModel>();

        private Task loadDataFromServerTask;

        private void InitTasks()
        {
            loadDataFromServerTask = new Task(() => ReadXMLAndCreatePredmeti(XDocument.Load("raspored.xml")));
            loadDataFromServerTask.ContinueWith((o) =>
                                                    {
                                                        NapraviPredmete();
                                                        NapraviVezbe();
                                                        lblLoading.Visibility = Visibility.Hidden;
                                                        canvas1.Visibility = Visibility.Hidden;
                                                    }, TaskScheduler.FromCurrentSynchronizationContext());
        }
        public pgNastava_i_vezbe()
        {
            InitializeComponent();
            loading();
            InitTasks();
            this.Loaded += pgNastava_i_vezbe_Loaded;
            _mainTimer = new DispatcherTimer();
            _mainTimer.Interval = new TimeSpan(0, 0, 0, 1);
            lblClock.Content = DateTime.Now.ToString(CultureInfo.CurrentCulture);
            _mainTimer.Tick += mainTimer_Tick;
            _mainTimer.Start();
        }

        #region Events for pgNastava_i_vezbe

        private void pgNastava_i_vezbe_Loaded(object sender, RoutedEventArgs e)
        {
            grid_ = Nastava.ActualWidth;
            loadDataFromServerTask.Start();
            Animate.SlideUp(this);
        }

        private void mainTimer_Tick(object sender, EventArgs e)
        {
            lblClock.Content = DateTime.Now.ToString();
            for (int i = 0; i < listaPredmeta.Count; i++)
            {
                listaPredmeta[i].View.changeTime();
                if (listaPredmeta[i].PreostaloVremena <= 0)
                {
                    Animate.RemoveSpecficChild(listaPredmeta[i].View, Nastava);
                    listaPredmeta.RemoveAt(i);
                }
            }
            for (int i = 0; i < listaVezbi.Count; i++)
            {
                listaVezbi[i].View.changeTime();
                if (listaVezbi[i].PreostaloVremena <= 0)
                {
                    Animate.RemoveSpecficChild(listaVezbi[i].View, Vezbe);
                    listaVezbi.RemoveAt(i);
                }
            }
        }

        private void btnNazad_Click(object sender, RoutedEventArgs e)
        {
            Animate.SlideDownExit(this);
        }

        #endregion

        private void ReadXMLAndCreatePredmeti(XDocument doc)
        {
            var saleNode = doc.Elements().First();
            foreach (var danNode in saleNode.Elements())
            {
                if (((string)danNode.Attribute("naziv"))==DateTime.Now.DayOfWeek.ToString()) {
                    foreach (var sala in danNode.Elements().Elements())
                    {
                        PredmetModel temp = new PredmetModel();
                        temp.SalaNaziv = (string)sala.Parent.Attribute("naziv");
                        temp.SalaTip = (string)sala.Parent.Attribute("tip");
                        temp.ImePredmeta = (string)sala;
                        //temp.PocetakPredavanja = DateTime.ParseExact((string)sala.Attribute("start"), "HH:mm",
                        //                                              CultureInfo.InvariantCulture);
                        //temp.KrajPredavanja = DateTime.ParseExact((string)sala.Attribute("end"), "HH:mm",
                        //                                              CultureInfo.InvariantCulture);
                        temp.PocetakPredavanja = TimeSpan.Parse((string)sala.Attribute("start"));
                        temp.KrajPredavanja = TimeSpan.Parse((string)sala.Attribute("end"));
                        temp.MaxWidth = grid_;

                            switch (temp.SalaTip)
                            {
                                case "nastava":
                                    listaPredmeta.Add(temp);
                                    break;
                                case "vezbe":
                                    listaVezbi.Add(temp);
                                    break;
                        }
                    }
                }
            }
        }
        
        
        void NapraviPredmete()
        {

            for (int i = 0; i < listaPredmeta.Count; i++)
            {

                var row2 = new RowDefinition();
                row2.Height = new GridLength(90);

                Nastava.RowDefinitions.Add(row2);

                Grid.SetRow(listaPredmeta[i].View, i + 1);

                Animate.AddChildAnimate(listaPredmeta[i].View, Nastava, i * 50);
            }
        }

        void NapraviVezbe()
        {
            for (int i = 0; i < listaVezbi.Count; i++)
            {
                var row2 = new RowDefinition();
                row2.Height = new GridLength(90);

                Vezbe.RowDefinitions.Add(row2);

                Grid.SetRow(listaVezbi[i].View, i + 1);

                Animate.AddChildAnimate(listaVezbi[i].View, Vezbe, i * 50);
            }
        }

        //Spinner draw
        void DrawCanvas()
        {
            for (int i = 0; i < 12; i++)
            {
                Line line = new Line()
                {
                    X1 = 50,
                    X2 = 50,
                    Y1 = 0,
                    Y2 = 20,
                    StrokeThickness = 5,
                    Stroke = Brushes.White,
                    Width = 100,
                    Height = 100
                };
                line.VerticalAlignment = VerticalAlignment.Center;
                line.HorizontalAlignment = HorizontalAlignment.Center;
                line.RenderTransformOrigin = new Point(.5, .5);
                line.RenderTransform = new RotateTransform(i * 30);
                line.Opacity = (double)i / 12;

                canvas1.Children.Add(line);
            }
        }

        void loading()
        {
            DrawCanvas();
            canvas2.Visibility = Visibility.Visible;
            DoubleAnimation a = new DoubleAnimation();
            a.From = 0;
            a.To = 360;
            a.RepeatBehavior = RepeatBehavior.Forever;
            a.SpeedRatio = 1;
            RotateTransform spin=new RotateTransform();
            spin.Angle = 0;
            canvas1.RenderTransform = spin;
            spin.BeginAnimation(RotateTransform.AngleProperty, a);
        }
    }
}
